#Imports
import pygame, sys
from pygame.locals import *
import random, time

#   2021-05-19 13：49   by 炸天帮成员
#   参考资料啥的见_2_pygame_demo_Part1_OK.py
#Initializing 
pygame.init()
 
#Setting up FPS 
FPS = 60
FramePerSec = pygame.time.Clock()
 
#Creating colors
BLUE  = (0, 0, 255)
RED   = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
 
#Other Variables for use in the program
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
SPEED = 5
 
#Create a white screen 
DISPLAYSURF = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("Game")
 
 
 
class Enemy(pygame.sprite.Sprite):
      def __init__(self):
        super().__init__() 
        self.image = pygame.image.load("enemy.png")
        self.surf = pygame.Surface((50, 80))
        self.rect = self.surf.get_rect(center = (random.randint(40,SCREEN_WIDTH-40), 0))       
 
      def move(self):
        self.rect.move_ip(0,SPEED)
        if (self.rect.top > 600):
            self.rect.top = 0
            self.rect.center = (random.randint(30, 370), 0)
 
 
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__() 
        self.image = pygame.image.load("download.png")
        self.surf = pygame.Surface((50, 29))
        self.rect = self.surf.get_rect(center = (0, 500))
        
    def move(self):
        pressed_keys = pygame.key.get_pressed()
       #if pressed_keys[K_UP]:
            #self.rect.move_ip(0, -5)
       #if pressed_keys[K_DOWN]:
            #self.rect.move_ip(0,5)
         
        if self.rect.left > 0:
              if pressed_keys[K_LEFT]:
                  self.rect.move_ip(-5, 0)
        if self.rect.right < SCREEN_WIDTH:        
              if pressed_keys[K_RIGHT]:
                  self.rect.move_ip(5, 0)
 
#Setting up Sprites        
P1 = Player()
E1 = Enemy()
 
#Creating Sprites Groups
enemies = pygame.sprite.Group() # 用于管理所有的名为enemies的pygame.sprite对象，合并成一个名为enemies的pygame.sprite.Group
enemies.add(E1)
print('enemies = ',enemies,' 类型为： ',type(enemies))
all_sprites = pygame.sprite.Group() # 用于管理所有的pygame.sprite对象，合并成一个名为all_sprites的pygame.sprite.Group
all_sprites.add(P1)
all_sprites.add(E1)
print('all_sprites = ',all_sprites,' 类型为： ',type(all_sprites))
 
#Adding a new User event 
INC_SPEED = pygame.USEREVENT + 1
print('INC_SPEED= ',INC_SPEED,'\n','INC_SPEED的类型为： ',type(INC_SPEED))
print('pygame.locals.QUIT= ',pygame.locals.QUIT)
pygame.time.set_timer(INC_SPEED, 1000)
 
#Game Loop
while True:
       
    #Cycles through all events occuring  
    for event in pygame.event.get():
        if event.type == INC_SPEED:
              SPEED += 2
           
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
 
 
    DISPLAYSURF.fill(WHITE)
 
    #Moves and Re-draws all Sprites
    for entity in all_sprites:
        DISPLAYSURF.blit(entity.image, entity.rect) #   将 名为 all_sprites 的pygame.sprite.Group组中的 pygame.Sprite 对象 装载 image皮肤。
        entity.move()   #   调用 名为 all_sprites 的pygame.sprite.Group组中的 pygame.Sprite 对象 的move()方法。
 
    #To be run if collision occurs between Player and Enemy
    if pygame.sprite.spritecollideany(P1, enemies): #   检测 名为P1的pygame.Sprite对象 是否 碰撞到了 名为enemies的pygame.sprite.Group组 的任意成员
          DISPLAYSURF.fill(RED) #   如果碰撞到了，则屏幕变红
          pygame.display.update()   #   屏幕显示效果刷新
          for entity in all_sprites:    #   清除所有的在 名为 all_sprites 的pygame.sprite.Group组中的 pygame.Sprite 对象。
                entity.kill() 
          time.sleep(2) #   暂停2s
          pygame.quit() #   结束pygame进程
          sys.exit()        
         
    pygame.display.update()
    FramePerSec.tick(FPS)